package cn.haohaowo.webservice.servlet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;

import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.service.invoker.BeanInvoker;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import cn.haohaowo.webservice.PassPortService;
import cn.haohaowo.webservice.PassPortServiceImpl;
import cn.haohaowo.webservice.server.ServerPasswordHandler;

public class WebServiceServletCheck extends CXFNonSpringServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7498020123006022138L;
	
	@Override
	protected void loadBus(ServletConfig sc) {
		super.loadBus(sc);
		System.out.println("############################");
		ServerFactoryBean serverFactoryBean = new ServerFactoryBean();
		serverFactoryBean.setServiceClass(PassPortService.class);
		serverFactoryBean.setAddress("/passPortService");
		PassPortService passPortService = new PassPortServiceImpl();
		serverFactoryBean.setInvoker(new BeanInvoker(passPortService));
		Server server = serverFactoryBean.create();
		Endpoint endpoint = server.getEndpoint();
		Map<String, Object> inProps = new HashMap<String, Object>();
		inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
		inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
		inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordHandler.class.getName());
		endpoint.getInInterceptors().add(new WSS4JInInterceptor(inProps));
	}

}
